package scfg.utils.trainging_results.delete;

import java.io.File;
import java.util.PriorityQueue;
import java.util.Scanner;

public class ResultsFile {
	private PriorityQueue<Run> runs;
	private File file;

	public void setPQ(PriorityQueue<Run> pq) {
		this.runs = pq;
	}

	public ResultsFile(File f) {
		this.file = f;
		runs = new PriorityQueue<Run>();
		processFile(f);
	}

	public ResultsFile(File f, PriorityQueue<Run> pq) {
		this.file = f;
		runs = pq;
	}

	public PriorityQueue<Run> getPQ() {
		PriorityQueue<Run> temp1 = new PriorityQueue<Run>();
		PriorityQueue<Run> temp2 = new PriorityQueue<Run>();
		while (!runs.isEmpty()) {
			Run r = runs.poll();
			temp1.offer(r);
			temp2.offer(r);
		}
		runs = temp1;
		return temp2;
	}

	private void processFile(File f) {
		try {
			Scanner scan = new Scanner(f);
			while (scan.hasNextLine()) {
				String line = scan.nextLine();
				if (line.startsWith("[Starting Training")) {
					String[] arr = line.split(" : ");
					String filename = arr[1].trim().substring(0,
							arr[1].length() - 1);
					File temp = new File(filename);
					if (f.exists())
						runs.offer(new Run(temp, scan));
				}
			}
			scan.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		PriorityQueue<Run> temp = new PriorityQueue<Run>();
		while (!runs.isEmpty()) {
			Run r = runs.poll();
			sb.append(r.toString()).append("\n\n");
			temp.offer(r);
		}
		runs = temp;
		return sb.toString();
	}
}
